﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core.Message;
using Core.ExceptionHandling;
using Core.Controls.Message;
using Core.Message.Entities;
using Core.Common.Session;
using Core.Controls.DataReader;
using Telerik.Web.UI;
using vHCS.SYS.Business;
using vHCS.HR.Business;

namespace vHCS.Web.MdlHR.Controls
{
    /// <summary>
    /// POPUP chọn nhân viên
    /// </summary>
    /// HUNGDV
    public partial class HR_PopupEmpSelect_Survey : Core.NTier.UserInterface.BaseForm
    {

        #region CONST
       
        //Hắng số Quẻy String _QS
        protected const string _QS_CALLER = "Caller";       
        //Các hằng số trên lưới _GR
        protected const string _GR_EMP_COLUMN = "EmployeeId";
        protected const string _QS_BARCODE = "Barcode";
        protected const string _QS_EMPCODE = "Empcode";
        protected const string _QS_EMPNAME = "Empname";
        protected const string _QS_SURVEYID = "SurveyID";
        //Hằng số khác
        protected const string _JS_SAVE_CALLBACK = "SaveCallback({0});";

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            //Response.ContentEncoding = System.Text.Encoding.Unicode;
            //UTF8Encoding utf8 = new UTF8Encoding();
            //UnicodeEncoding unicode = new UnicodeEncoding();


            if (!IsPostBack)
            {
                long lCallerId = 0;
                long.TryParse(Request.QueryString[_QS_CALLER], out lCallerId);
                this.hdfCallerId.Value = lCallerId.ToString();


                //get survey id
                long lSurveyId = 0;
                long.TryParse(Request.QueryString[_QS_SURVEYID], out lSurveyId);
                this.hdfSurveyId.Value = lSurveyId.ToString();

                //Gán các giá trị từ form cha
                if (Request.QueryString[_QS_EMPCODE] != null)
                {
                    this.ucEmpSearch.EmpCode = Request.QueryString[_QS_EMPCODE];
                }
                if (Request.QueryString[_QS_EMPNAME] != null)
                {
                    string sEmpName = Request.QueryString[_QS_EMPNAME];

                    sEmpName = Server.UrlDecode(sEmpName);

                    this.ucEmpSearch.EmpName = sEmpName;
                }
                if (Request.QueryString[_QS_BARCODE] != null)
                {
                    this.ucEmpSearch.Barcode = Request.QueryString[_QS_BARCODE];
                }

                this.SetMultiChoice();
                this.SetCompleteType();
            }
        }

        #region Method For Page_Load
        /// <summary>
        /// Gán trạng thái form là Save hay Chọn
        /// </summary>
        protected void SetCompleteType() 
        {
           try{
                string strIsSave = this.Request.QueryString["IsSave"];
                bool bSaveVisible = false;
                if (strIsSave == "1") bSaveVisible = true;
               
                this.btnSave.Visible = this.btnSaveBot.Visible = bSaveVisible;
                this.btnSelect.Visible = this.btnSelectBot.Visible = !bSaveVisible;
            }
            catch (Exception ex)
            {
                AlertMessage mess = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(mess);
            }
        }

        /// <summary>
        /// Cho phép chọn nhiều hoặc chọn 1 nhân viên trên lưới
        /// </summary>
        protected void SetMultiChoice() {
            try
            {
                string mChoise = this.Request.QueryString["mChoice"];
                if (string.IsNullOrEmpty(mChoise)) mChoise = "0";
                if (mChoise.Trim() == "1") dtgEmpList.AllowMultiRowSelection = true;
                else dtgEmpList.AllowMultiRowSelection = false;
            }
            catch (Exception ex)
            {
                AlertMessage message = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(message);
            }
        }
        #endregion

        #region Form Event
        /// <summary>
        /// Tìm kiếm nhân viên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try {
                object obj=this.dtgEmpList.DataSourceObject;
                if (obj != null && obj is CoreDataSource)
                {
                    CoreDataSource dts = (CoreDataSource)obj;
                    dts.CacheClear();
                }
                base.ClearGridFilter(this.dtgEmpList);
                this.dtgEmpList.DataBind();
            }
            catch (Exception ex)
            {
                AlertMessage message = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(message);
            }
        }
        public void ReturnSelectedEmps() {
            try
            {
                string strArrayArgs = string.Empty;
                foreach (GridDataItem row in this.dtgEmpList.SelectedItems)
                {
                    if (row is GridDataItem)
                    {
                        GridDataItem item = (GridDataItem)row;
                        long empId = 0;
                        long.TryParse(row[_GR_EMP_COLUMN].Text.Trim(), out empId);
                        if (empId > 0)
                        {
                            if (string.IsNullOrEmpty(strArrayArgs) == false) strArrayArgs += ",";
                            strArrayArgs += string.Format("'{0}'", empId);
                        }

                    }
                }
                if (string.IsNullOrEmpty(strArrayArgs) == false)
                    strArrayArgs = string.Format("[{0}]", strArrayArgs);

                //Sử dụng Javacript load các form cha
                //Trả về danh sách mã nhân viên (EmployeeId): ['EmpId1','EmpId2']
                ClientScript.RegisterStartupScript(Page.GetType(), "EmpSelected", string.Format(_JS_SAVE_CALLBACK, strArrayArgs), true);
            }
            catch (Exception ex)
            {
                AlertMessage mess = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(mess);
            }
        }
        /// <summary>
        /// Click nút chọn nhân viên
        /// Trả về danh sách mã nhân viên (EmployeeId): ['EmpId1','EmpId2']
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            this.ReturnSelectedEmps();
        }

        /// <summary>
        /// Click nút lưu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.ReturnSelectedEmps();
        }
        #endregion

        #region  Private Logic Method
     
        private List<long> SelectedEmployees()
        {
            List<long> lstEmps = new List<long>();
            foreach (GridDataItem row in this.dtgEmpList.SelectedItems)
            {
                if (row is GridDataItem)
                {
                    GridDataItem item = (GridDataItem)row;
                    long empId = 0;
                    long.TryParse(row[_GR_EMP_COLUMN].Text.Trim(), out empId);
                    if (empId > 0) lstEmps.Add(empId);

                }
            }
            return lstEmps;
        }
      
        #endregion

        protected void btnReset_Click(object sender, EventArgs e)
        {
            try
            {
                this.ucEmpSearch.ResetForm();               
                this.dtsLoadAllEmp.CacheClear();               
                this.ClearGridFilter(this.dtgEmpList);
                this.dtgEmpList.DataBind();
            }
            catch (Exception ex)
            {
                AlertMessage mess = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(mess);
            }
        }
    }
}